package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreFollowType;
import com.dingreading.cloud.admin.entity.table.StoreFollowTypeTableDef;
import com.dingreading.cloud.admin.mapper.StoreFollowTypeMapper;
import com.dingreading.cloud.admin.service.StoreFollowTypeService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-12-03
 */
@Service
public class StoreFollowTypeServiceImpl extends BaseServiceImpl<StoreFollowTypeMapper, StoreFollowType, StoreFollowTypeTableDef> implements StoreFollowTypeService {

    @Override
    protected StoreFollowTypeTableDef getTable() {
        return StoreFollowTypeTableDef.storeFollowType;
    }

    @Override
    public Page<StoreFollowType> pageList(PageUtil pageUtil, String keyword, Integer source, Integer operate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (source != null)
            condition.and(table.source.eq(source));
        if (operate != null)
            condition.and(table.operation.eq(operate));

        QueryWrapper wrapper = QueryWrapper.create().where(condition).orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public boolean haveName(Long id, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (id != null)
            condition.and(table.id.ne(id));
        condition.and(table.name.eq(name));
        return exists(condition);
    }

    @Override
    public StoreFollowType getByUid(String followTypeUid) {
        return getMapper().selectOneByCondition(table.uid.eq(followTypeUid));
    }

    @Override
    public List<StoreFollowType> listBySource() {
        return getMapper().selectListByCondition(table.source.eq(2));
    }

}
